package org.jeecg.common.util.pay.wx;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: Planifolia.Van
 * @Date: 2025/1/9 09:15
 */
@Component
public class WxPayConfigure {
    /**
     * 构建微信支付Config
     *
     * @param wxPayProperties 微信配置信息
     * @return 构建结果
     */
    @Bean
    public Config wxPayConfig(WxPayProperties wxPayProperties) {
        // 微信支付证书配置
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(wxPayProperties.getSpMchid())
                .privateKeyFromPath(wxPayProperties.getPrivateKeyPath())
                .merchantSerialNumber(wxPayProperties.getMerchantSerialNumber())
                .apiV3Key(wxPayProperties.getApiV3Key())
                .build();
    }

    /**
     * 构建微信回调解析器
     *
     * @param wxPayConfig 微信支付配置
     * @return 构建出来的回调解析器
     */
    @Bean
    @DependsOn("wxPayConfig")
    public NotificationParser wxNotifyParser(Config wxPayConfig) {
        return new NotificationParser((NotificationConfig) wxPayConfig);
    }

}
